package org.jeecg.modules.oa.controller.appController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.oa.entity.SignInManagement;
import org.jeecg.modules.oa.entity.app.SignInRequestEntity;
import org.jeecg.modules.oa.service.ISignInManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/b/sign/app")
public class AppSignInController {

	@Autowired
	private ISignInManagementService signInManagementService;

	/**
	 * 签到保存接口
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public Result save(SignInRequestEntity signInRequestEntity) {
		LoginUser user = SsoUtil.getLoginUser();
		if (user == null || StringUtils.isBlank(user.getId())) {
			return Result.error(99,"登录失效");
		}
		SignInManagement newSignIn = new SignInManagement();
		// 设置用户编号
		newSignIn.setUserId(user.getId());
		newSignIn.setInLongitude(signInRequestEntity.getSignLongitude());
		newSignIn.setInLatitude(signInRequestEntity.getSignLatitude());
		newSignIn.setImageUrl(signInRequestEntity.getSignPicUrl());
		newSignIn.setSignInHouse(signInRequestEntity.getSignHouse());
		newSignIn.setSignInPosition(signInRequestEntity.getSignPosition());
		newSignIn.setSignInTime(new Date());
		newSignIn.setRemarks(signInRequestEntity.getSignRemarks());
		signInManagementService.save(newSignIn);
		return Result.ok(0,"签到成功");
	}

	/**
	 * 签到次数查询接口
	 */
	@GetMapping(value = "queryCount")
	public Result queryCount(SignInRequestEntity signInRequestEntity) {
		SignInManagement signIn = new SignInManagement();
		Date checkDate;
		LoginUser user = SsoUtil.getLoginUser();
		if (user == null || StringUtils.isBlank(user.getId())) {
			return Result.error(99,"登录失效");
		}
		// 查询日期
		try {
			checkDate = DateUtils.parseDate(signInRequestEntity.getCheckDate(),"yyyy-MM-dd");
			// 设置当前系统时间的当天开始时间
			signIn.setSignInTime(DateUtils.getDateStart(checkDate));
		} catch (ParseException e) {
			log.error(e.getMessage(), e);
			return Result.error(1,"系统繁忙！");
		}
		// 查询签到次数
		signIn.setUserId(user.getId());
		int checkNumber = signInManagementService.findCheckCount(signIn);
		return Result.ok(0,checkNumber,"获取数据成功");
	}

	/**
	 * 足迹查询接口
	 */
	@GetMapping(value = "footprint")
	public Result footprint(SignInRequestEntity signInRequestEntity, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
							@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		SignInManagement signIn = new SignInManagement();
		try {
			LoginUser user = SsoUtil.getLoginUser();
			if (user == null || StringUtils.isBlank(user.getId())) {
				return Result.error(99,"登录失效");
			}
			if(StringUtils.isNotEmpty(signInRequestEntity.getCheckDate())){
				// 查询日期
				Date checkDate = DateUtils.parseDate(signInRequestEntity.getCheckDate(),"yyyy-MM-dd");
				// 设置当前系统时间的当天开始时间
				signIn.setSignInTime(DateUtils.getDateStart(checkDate));
			}
			// 设置筛选用户条件
			signIn.setUserId(user.getId());
			Page<SignInManagement> page = new Page<SignInManagement>(pageNo, pageSize);
			List<SignInManagement> list = signInManagementService.findByUserId(page,signIn);
			page.setRecords(list);
			// 查询签到次数
//			Page<SignInManagement> page = signInService.findPage(new Page<SignInManagement>(request,response),signIn);
			return Result.ok(page,"查询成功");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return Result.error(1,"系统繁忙！");
		}
	}

}
